//extend booky with a window plugin
booky.fn.formSerialize = function(str) {

    var els = []
        bits = {},    
        formBits = this.sel("input, textarea, select", this[0].parentNode); //need to think about this, is that good enough?
  
    for(var i=0;i<formBits.length;i++) 
        if(formBits[i].type != "button")
            els.push(formBits[i]);
     
    //return as string   
    if(str) {
        str = "";
        for(var i=0;i<els.length;i++)  {
            if(els[i].type === "checkbox") if(!els[i].checked) continue;   
           
                                
            if(els[i].nodeName === "SELECT") {              

                var opts = this.sel("option", els[i].parentNode),
                    optsStr="";
                for(var o=0;o<opts.length;o++) {
                    if(opts[o].selected) {
                        optsStr += opts[o].value + (o==opts.length-1 ? "" : ","); 
                    }
                }
                add(optsStr);
                continue;
           } 
           
            
           //normal value       
           add();
        }
        return str;
    }
    
    function add(s) {
        str += els[i].name + "=" + (typeof s === "undefined" ? encodeURIComponent(els[i].value) : s) + (i==els.length-1 ? "" : "&"); 
    }
    
    
    //json
    for(var i=0;i<els.length;i++) {
        bits[els[i].name] = els[i].value;
    }         
    
    return bits;
}





//validation
booky.validations = {
    notEmpty: function(e) {
        return e.value.length > 0;
    },
    checkboxChecked: function(e) {
        return e.checked; //remember normal styling doesn't always show up on checkboxes
    },
    telephone: function(e) {
        return e.value.match(/\b[0-9]{13}\b/);
    },
    //ty jshanman  http://www.webmasterworld.com/forum91/5611.htm
    password: function(e) {
        return e.value.match(/[a-z]/) && s.match(/[A-Z]/) && s.match(/[0-9]/);
    },
    email: function(e) {//doesnt validate all emails..allow chars like '
        return e.value.match(/^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/);             ///need to enable extending this    , perhaps iwth just a settings function...
    },
    isUrl: function(e) {
        return  e.value.match(/^(?:http:\/\/)?(?:[\w-]+\.)+[a-z]{2,6}$/i); //needs new one
    }
};

//basic form that just checks the form every 
booky.fn.clientValidate = function(validations) {  //NEED TO DISABLE THE SUBMIT  BUTTON.........
    validations = booky.fn.merge(booky.validations, validations);
    
    var _self = this,
        submit = this.sel(".submit")[0],         //must fix find:
        
        timer = setInterval(function() {            
            submit.disabled = false;
            _self.each( function() {
                for(test in booky.validations) {
                    if(booky(this).hasClass(test))  {
                        if(booky.validations[test](this))
                            booky(this).addClass("passed").removeClass("failed");            
                        else {
                            booky(this).addClass("failed").removeClass("passed");
                            submit.disabled = true;
                        }                            
                    }
                }                
            });
            
        }, 100); 
    return this;
}